package org.tloss.web.editor.database.editors;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DatabaseConnect {
	public Properties databaseConfig = new Properties();
	public ClassLoader jdbcClassLoader;

	public DatabaseConnect(String url, String user, String password,
			String driverClass, ClassLoader jdbcClassLoader) {
		this.jdbcClassLoader = jdbcClassLoader;
		if (user != null) {
			databaseConfig.put("user", user);
		}
		if (password != null) {
			databaseConfig.put("password", password);
		}
		databaseConfig.put("driverClass", driverClass);
		databaseConfig.put("jdbcURL", url);
	}

	public Connection getConnection() throws SQLException,
			ClassNotFoundException, InstantiationException,
			IllegalAccessException {

		Class<?> c = Class.forName(databaseConfig.getProperty("driverClass"),
				true, jdbcClassLoader);
		DriverManager.registerDriver(new DelegatingDriver((Driver) c
				.newInstance()));
		return DriverManager.getConnection(
				databaseConfig.getProperty("jdbcURL"), databaseConfig);
	}

}
